<?php
namespace app\massage\model;

use app\BaseModel;
use think\facade\Db;

class CarGame extends BaseModel
{
    //定义表名
    protected $name = 'shequshop_car_game';


    protected $append = [

        'start_time_text',

        'best_time_text',

        'total_time_text',

    ];


    /**
     * @author chenniang
     * @DataTime: 2021-10-21 10:20
     * @功能说明:转换时间格式
     */
    public function getBestTimeTextAttr($value,$data){

        if(!empty($data['best_time'])){

           return game_time($data['best_time']);

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2021-10-21 10:20
     * @功能说明:转换时间格式
     */
    public function getTotalTimeTextAttr($value,$data){

        if(!empty($data['total_time'])){

            return game_time($data['total_time']);

        }

    }

    /**
     * @author chenniang
     * @DataTime: 2021-09-28 11:48
     * @功能说明:转换时间格式
     */
    public function getStartTimeTextAttr($value,$data){

        if(!empty($data['start_time'])){

            return date('Y.m.d H:i',$data['start_time']);
        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }



    /**
     * @author chenniang
     * @DataTime: 2021-09-16 13:57
     * @功能说明:成绩排行版
     */
    public function topRecordList($dis,$page=10,$mapor=[]){

        $data = $this->alias('a')
            ->join('massage_service_user_list b','a.user_id = b.id')
            ->where($dis)
            ->where(function ($query) use ($mapor){
                $query->whereOr($mapor);
            })
            ->field('a.*,b.nickName,b.avatarUrl')
            ->group('a.id')
            ->order('a.best_time asc,a.id desc')
            ->paginate($page)
            ->toArray();


        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2021-09-16 13:57
     * @功能说明:成绩排行版(可根据时间)
     */
    public function timeTopRecordList($dis,$page=10,$time = 1,$mapor=[]){

        switch ($time){

            case 1:

                $time_text = 'today';

                break;
            case 2:

                $time_text = 'week';

                break;
            case 3:

                $time_text = 'month';

                break;
            case 4:

                $time_text = 'year';

                break;
        }

        $data = $this->alias('a')
            ->join('massage_service_user_list b','a.user_id = b.id')
            ->where($dis)
            ->whereTime('a.start_time',$time_text)
            ->where(function ($query) use ($mapor){
                $query->whereOr($mapor);
            })
            ->field('a.*,b.nickName,b.avatarUrl')
            ->group('a.id')
            ->order('a.best_time asc,a.id desc')
            ->paginate($page)
            ->toArray();



        return $data;

    }







}